
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;

public class SemanticaUtils {
	
	private static SemanticaUtils instance;
	private Stack<Integer> pilhaIndent;
	private int currentIndent;
	public Set<String> variaveis;
	private Map<String, Integer> funcoes;
	public Set<String> listaIdFuncoes;
	
	private SemanticaUtils() {
		pilhaIndent = new Stack<Integer>();
		currentIndent = 0;
		pilhaIndent.push(currentIndent);
		variaveis = new HashSet<String>();
		funcoes = new HashMap<String, Integer>();
		listaIdFuncoes = new HashSet<String>();
	}
	
	public void empilha() {
		currentIndent++;
		pilhaIndent.push(currentIndent);
	}
	
	public void desempilha() {
		if (currentIndent < 0) {
			return;
		}
		currentIndent--;
		pilhaIndent.pop();
	}
	
	public boolean existeVariavel(String variavel) {
		return variaveis.contains(variavel);
	}
	
	public void adicionaVariavel(String variavel){
		variaveis.add(variavel);
	}
	
	public void adicionaFuncao(String symbol, int numParametros){
		funcoes.put(symbol, numParametros);
	}
	
	public void adicionaDefinicaoFuncao(String n) throws Exception{
		if (listaIdFuncoes.contains(n)) {
			throw new Exception("Nome de funcao ja definido");
		}
		listaIdFuncoes.add(n);
	}
	
	public boolean existeFuncao(Integer idFuncao){
		return funcoes.containsKey(idFuncao);
	}
	
	public Integer numParametrosFuncao(String idFuncao){
		return funcoes.get(idFuncao);
	}
	
	public int getCurrentIndent() {
		return currentIndent;
	}
	
	public static SemanticaUtils getInstance() {
		if (instance == null) {
			instance = new SemanticaUtils();
		}
		return instance;
	}
	
	public void limpaTudo(){
		variaveis.clear();
		funcoes.clear();
	}
	
}
